package com.nexgo.payment.action.scan;

import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.nexgo.payment.R;
import com.nexgo.payment.action.Action;
import com.nexgo.payment.action.ActionResult;
import com.nexgo.payment.action.ActionStep;
import com.nexgo.payment.action.OnActionListener;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.ui.BaseFragment;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.widget.TitleBuilder;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import java8.util.Optional;

/**
 * 扫码界面
 *
 * Created by wanghongyang on 2017/9/1.
 */
public class ScanFragment extends BaseFragment implements Action<ActionStep>, ScanContract.View {

    private ScanContract.Presenter mPresenter;
    private OnActionListener mListener;
    private Unbinder unbinder;

    public ScanFragment() {
    }

    public static ScanFragment newInstance() {
        ScanFragment fragment = new ScanFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //强制屏幕常亮
        Window win = getActivity().getWindow();
        win.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//        //屏蔽菜单键
//        win.addFlags(5);
//        //屏蔽home键
//        win.addFlags(3);
//        //屏蔽下拉
//        Settings.System.putInt(getContext().getContentResolver(), "status_bar_disabled", 1);
        new ScanPresenter(this, Repository.getInstance());
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_input_scan, container, false);
        unbinder = ButterKnife.bind(this, view);

        initTitle(view);

        return view;
    }

    //初始化标题栏
    private void initTitle(View view) {
        new TitleBuilder(view)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setTitleText(getString(R.string.input_scan))
                .build();
    }

    @Override
    protected void initEvent() {
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnActionListener) {
            mListener = (OnActionListener) context;
        }
    }

    @Override
    public ActionStep getActionStep() {
        return ActionStep.INPUT_SCAN;
    }

    @Override
    public void setPresenter(ScanContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showErrMsg(String message) {
        Optional.ofNullable(message)
                .filter(s -> !TextUtils.isEmpty(s))
                .ifPresent(s -> ToastUtils.showToast(s));
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.ERR_CANCEL, message));
        }
    }

    @Override
    public void showScanSuccessful(String data) {
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.SUCC, data));
        }
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        mPresenter.destroy();
//        Settings.System.putInt(getContext().getContentResolver(), "status_bar_disabled", 0);
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

}
